package io.kiki.sba.registry.server.metadata.resource;

import io.kiki.sba.registry.common.model.console.PersistenceData;
import io.kiki.sba.registry.common.model.constants.ValueConstants;
import io.kiki.sba.registry.server.metadata.provide.data.ProvideDataService;
import io.kiki.sba.registry.server.metadata.resource.filter.LeaderAwareRestController;
import io.kiki.sba.registry.store.api.DBResponse;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shangyu.wh
 * @version $Id: RenewSwitchResource.java, v 0.1 2018-07-25 11:40 shangyu.wh Exp $
 */
@Path("slotSync")
@LeaderAwareRestController
public class SlotSyncResource {

    @Autowired
    private ProvideDataService provideDataService;

    private static String getEntityData(DBResponse<PersistenceData> resp) {
        if (resp != null && resp.getEntity() != null) {
            return resp.getEntity().getData();
        }
        return "null";
    }

    @GET
    @Path("get")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getSlotSync() throws Exception {
        Map<String, Object> resultMap = new HashMap<>(2);
        DBResponse<PersistenceData> syncSessionIntervalSec = provideDataService.queryProvideData(ValueConstants.DATA_DATUM_SYNC_SESSION_INTERVAL_SEC);
        DBResponse<PersistenceData> dataDatumExpire = provideDataService.queryProvideData(ValueConstants.DATA_SESSION_LEASE_SEC);

        resultMap.put("syncSessionIntervalSec", getEntityData(syncSessionIntervalSec));
        resultMap.put("dataDatumExpire", getEntityData(dataDatumExpire));
        return resultMap;
    }


    protected SlotSyncResource setProvideDataService(ProvideDataService provideDataService) {
        this.provideDataService = provideDataService;
        return this;
    }
}
